using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BlackJackSimulator
    {
    public partial class frmMain : Form
        {
        public frmMain()
            {

            InitializeComponent();
            }
        List<Game> GameSim = new List<Game>();
        System.Windows.Forms.ProgressBar[] pbDeckTracker;


        private void RunGames()
            {
            this.pbRuns.Value = 0;
            this.butProcess.Text = "Pause";
            this.butProcess.Refresh();

            ResetDecks();

            this.pbRuns.Maximum = Convert.ToInt32(this.txtNumRuns.Text.ToString());

            GameSim[0].GameDeck.Clear();
           // SetNewGame();
           // LoadPlayerGrid();


            int intPlayerCount = 0;
            intPlayerCount = GameSim[0].PlayerSummaries.Count;

            if (intPlayerCount < 2)
                {
                MessageBox.Show("You must have at least 1 player against the dealer","No Players");
                return;
                }
            GameSim[0].Exec(Convert.ToInt32(numDecks.Text),
                Convert.ToInt32(intPlayerCount),
                Convert.ToInt32(this.numHands.Text),
                Convert.ToInt32(txtNumRuns.Text));
            this.dgResults.Enabled = true;

            this.dgResults.DataSource = GameSim[0].dsResults;
            this.butProcess.Text = "Process";
            }

        private void SetNewGame()
            {
            GameSim.Clear();
            GameSim.Add(new Game());
            this.GameSim[0].GameProgress += new EventHandler(frmMain_GameProgress);
            this.GameSim[0].DeckIDUsed += new EventHandler(frmMain_DeckIDUsed);
            this.GameSim[0].DecksReset += new EventHandler(frmMain_DecksReset);
            this.dgResults.Enabled = false;

            }

        private void ResetDecks()
            {
            this.grpDeckTracker.Controls.Clear();
            pbDeckTracker = new ProgressBar[System.Convert.ToInt32(this.numDecks.Text)];

            for (int iDeckTracker = 0; iDeckTracker < System.Convert.ToInt32(this.numDecks.Text); iDeckTracker++)
                {
                pbDeckTracker[iDeckTracker] = new ProgressBar();
                pbDeckTracker[iDeckTracker].Maximum = 52;
                grpDeckTracker.Controls.Add(pbDeckTracker[iDeckTracker]);
                grpDeckTracker.Controls[iDeckTracker].Top = (pbDeckTracker[iDeckTracker].Height * iDeckTracker) + 20;
                pbDeckTracker[iDeckTracker].Width = 100;
                pbDeckTracker[iDeckTracker].Tag = "Deck: " + iDeckTracker.ToString();
                toolTip1.SetToolTip(pbDeckTracker[iDeckTracker],"Deck: " + iDeckTracker.ToString());
                }
            }

        void frmMain_DecksReset(object sender,EventArgs e)
            {
            for (int iDeckTracker = 0; iDeckTracker < System.Convert.ToInt32(this.numDecks.Text); iDeckTracker++)
                {
                pbDeckTracker[iDeckTracker].Value = 0;

                }
            }

        void frmMain_DeckIDUsed(object sender,EventArgs e)
            {
            int intDeckIDUSed = (int)sender;
            this.pbDeckTracker[intDeckIDUSed - 1].Increment(1);
            toolTip1.SetToolTip(pbDeckTracker[intDeckIDUSed - 1],"Deck: " + intDeckIDUSed.ToString() + " : " + pbDeckTracker[intDeckIDUSed - 1].Value.ToString());
            }

        void frmMain_GameProgress(object sender,EventArgs e)
            {
            this.pbRuns.Increment(1);
            this.dgvPlayerSetup.Refresh();
            Application.DoEvents();
            }



        private void processToolStripMenuItem_Click(object sender,EventArgs e)
            {
            RunGames();
            }

        private void processToolStripMenuItem_Click_1(object sender,EventArgs e)
            {
            RunGames();
            }

        private void viewPlayerMatrixToolStripMenuItem_Click(object sender,EventArgs e)
            {
            frmPlayerActionMatrix frmMatrix = new frmPlayerActionMatrix();
            frmMatrix.Show();
            }

        private void dgResults_Navigate(object sender,NavigateEventArgs ne)
            {

            }

        private void frmMain_Load(object sender,EventArgs e)
            {
            SetNewGame();
            LoadPlayerGrid();
            }
        private void LoadPlayerGrid()
            {

            if (GameSim[0].PlayerSummaries.Count == 0)
                {
                GameSim[0].PlayerSummaries.Add(new Player(1));
                GameSim[0].PlayerSummaries[0].IsPlayerDealer = true;

                }

            GameSim[0].PlayerSummaries.Add(new Player(GameSim[0].PlayerSummaries.Count + 1));
            foreach (Player CheckPlayerForPlayerMatrix in GameSim[0].PlayerSummaries.FindAll(delegate(Player p) { return !p.IsPlayerDealer; }))
                {

                if (CheckPlayerForPlayerMatrix.PlayerMatrixID != "")
                    {
                    GameSim[0].PlayerSummaries[CheckPlayerForPlayerMatrix.PlayerID].LoadPlayerMatrix(GameSim[0].PlayerSummaries[CheckPlayerForPlayerMatrix.PlayerID].PlayerMatrixID);
                    dgvPlayerSetup[0, CheckPlayerForPlayerMatrix.PlayerID].Value = GameSim[0].PlayerSummaries[CheckPlayerForPlayerMatrix.PlayerID].PlayerMatrixID;
                    }

                }

            dgvPlayerSetup.DataSource = GameSim[0].PlayerSummaries;
            this.dgvPlayerSetup.Columns["CurrentPlayerMatrix"].Visible = false;
            this.dgvPlayerSetup.Columns["DidPlayerWin"].Visible = false;
            this.dgvPlayerSetup.Columns["DidPlayerWin"].Visible = false;
            }

        private void dgvPlayerSetup_RowsAdded(object sender,DataGridViewRowsAddedEventArgs e)
            {

            }

        private void butProcess_Click(object sender,EventArgs e)
            {
            if (this.butProcess.Text == "Process")
                {
                foreach (Player CheckPlayerForPlayerMatrix in GameSim[0].PlayerSummaries.FindAll(delegate (Player p) {return !p.IsPlayerDealer;}))
                    {
                    
                    if (CheckPlayerForPlayerMatrix.PlayerMatrixID == ""                        )
                        {
                        MessageBox.Show("No Player Matrix for Player " + CheckPlayerForPlayerMatrix.PlayerID.ToString());
                        return;


                        }
                    
                    }
                RunGames();
                }
            else
                {

                GameSim[0].StopRuns = true;

                }
            }

        private void dgvPlayerSetup_CellContentClick(object sender, DataGridViewCellEventArgs e)
            {
            if (e.RowIndex > 0 && e.ColumnIndex==0
                )
                {
                if (fdSelectPlayerMatrix.ShowDialog() == DialogResult.OK)
                    {
                    GameSim[0].PlayerSummaries[e.RowIndex].LoadPlayerMatrix(fdSelectPlayerMatrix.FileName);
                    dgvPlayerSetup[0, e.RowIndex].Value = fdSelectPlayerMatrix.FileName;
                    dgvPlayerSetup[1, e.RowIndex].Style.BackColor = Color.LightGreen;


                    }

                }
            }

        private void dgvPlayerSetup_DataMemberChanged(object sender, EventArgs e)
            {
            

            }

        private void butReset_Click(object sender, EventArgs e)
            {
            SetNewGame();
            LoadPlayerGrid();
            }

        private void saveToolStripMenuItem1_Click(object sender, EventArgs e)
            {
            
            }


        }
    }